package org.dreamwork.drawing.framework;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: seth.yang
 * Date: 2010-8-3
 * Time: 15:21:33
 */
public class JCanvas extends JComponent implements Serializable {
    private IGraph graph;
    private IElementRendererFactory factory;

    public JCanvas (IGraph graph, IElementRendererFactory factory) {
        this.graph = graph;
        this.factory = factory;
    }

    public void add (IVertex vertex) {
        graph.add (vertex);
    }

    public void add (IVertex... vertexes) {
        graph.add (vertexes);
    }

    public void add (List<IVertex> vertexes) {
        graph.add (vertexes);
    }

    public void add (IEdge edge) {
        graph.add (edge);
    }

    public void add (IEdge... edges) {
        graph.add (edges);
    }

    public void add (Collection<IEdge> edges) {
        graph.add (edges);
    }

    public void remove (IVertex vertex) {
        graph.remove (vertex);
    }

    public void remove (IVertex... vertexes) {
        graph.remove (vertexes);
    }

    public void remove (List<IVertex> vertexes) {
        graph.remove (vertexes);
    }

    public void remove (IEdge edge) {
        graph.remove (edge);
    }

    public void remove (IEdge... edges) {
        graph.remove (edges);
    }

    public void remove (Collection<IEdge> edges) {
        graph.remove (edges);
    }

    @Override
    public void paint (Graphics g) {
        BufferedImage image = new BufferedImage (getWidth (), getHeight (), BufferedImage.TYPE_4BYTE_ABGR);
        Graphics2D g2 = (Graphics2D) image.getGraphics ();
        for (IVertex vertex : graph.getVertexes ()) {
            JComponent c = factory.getRenderer (vertex).getRendererComponent (vertex, false);
            c.paint (g2);
        }

        for (IEdge edge : graph.getEdges ()) {
            JComponent c = factory.getRenderer (edge).getRendererComponet (edge, false);
            c.paint (g2);
        }
        g.drawImage (image, 0, 0, this);
    }
}